package top.gangster.lunan.contractservice.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.gangster.lunan.contractservice.entity.Client;
import top.gangster.lunan.contractservice.service.ClientService;
import top.gangster.lunan.result.Result;

@RestController
@RequestMapping("/contractservice/client")
public class ClientController {
	@Autowired
	ClientService clientService;

	/**
	 * 获取所有客商信息
	 * @return 所有客商信息
	 */
	@GetMapping
	public Result getAllClient() {
		List<Client> clientList = clientService.list(null);
		return Result.ok().data("clients",clientList);
	}

	/**
	 * 获取指定id的客商
	 * @param clientId 客商id
	 * @return 客商对象
	 */
	@GetMapping("/{clientId}")
	public Result getClientByClientId(@PathVariable String clientId) {
		Client client = clientService.getById(clientId);
		return Result.ok().data("client",client);
	}

	/**
	 * 保存客商信息
	 * @param client 待保存的客商对象
	 * @return Result
	 */
	@PostMapping
	public Result saveClient(@RequestBody Client client) {
		boolean b = clientService.save(client);
		if (b) {
			return Result.ok().message("添加客商成功");
		}
		return Result.error().message("添加客商失败");
	}

	/**
	 * <h2>该方法不允许被直接调用，需要在删除合同信息时被调用</h2>
	 * 删除客商信息
	 * @param clientId 待删除客商的id
	 * @return Result
	 */
	@DeleteMapping("/{clientId}")
	public Result deleteClient(@PathVariable String clientId) {
		boolean b = clientService.removeById(clientId);
		if (b) {
			return Result.ok().message("删除客商信息成功");
		}
		return Result.error().message("删除客商信息失败");
	}

	/**
	 * 更新客商信息
	 * @param client 更新后的客商对象
	 * @return Result
	 */
	@PutMapping
	public Result updateClient(@RequestBody Client client) {
		boolean b = clientService.updateById(client);
		if (b) {
			return Result.ok().message("更新客商信息成功");
		}
		return Result.error().message("更新客商信息失败");
	}
}
